////////////////////////////////////////////////////////////////////////////////
//
//	This file is part of Woozox, see <http://code.google.com/p/woozox/>.
//
//	Copyright 2008-2009 Florian David
//
//	Woozoo is free software: you can redistribute it and/or modify
//	it under the terms of the GNU Lesser General Public License as 
//	published by the Free Software Foundation, either version 3 of 
//	the License, or (at your option) any later version.
//
//	Woozoo is distributed in the hope that it will be useful,
//	but WITHOUT ANY WARRANTY; without even the implied warranty of
//	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
//	GNU General Public License for more details.
//
//	You should have received a copy of the GNU Lesser General Public 
//	License along with Woozoo. If not, see <http://www.gnu.org/licenses/>.
//
////////////////////////////////////////////////////////////////////////////////
package org.woozoo.xhtml.wordpress.base
{
	import org.woozoo.xhtml.core.base.Element;
	import org.woozoo.xhtml.core.manager.*;
	import org.woozoo.xhtml.core.manager.document.DocumentManager;
	import org.woozoo.xhtml.core.util.StringUtil;
	import org.woozoo.xhtml.extended.base.*;
	import org.woozoo.xhtml.wordpress.event.WPCommentEvent;
	import org.woozoo.xhtml.wordpress.theme.WPTheme;
	import org.woozoo.xhtml.wordpress.util.WPDateUtil; 
	
	/**
 	 * @private
	 * */ 
	[Event(name="commentSucceed", type="org.woozoo.xhtml.wordpress.event.WPCommentEvent")]

	/**
 	 * @private
	 * */ 
	[Event(name="commentFailed", type="org.woozoo.xhtml.wordpress.event.WPCommentEvent")]

	/**
	 * This class represent a post from your wordpress blog
	 * */
	public class WPPost extends WPDocument
	{
		private var __title : String;
		private var __date : Date;
		private var __content : Element;
		private var __numComments : int = -1;
 		private var __postId : int = -1;
		private var __comments : Array = []; /* of WPComment */
		// Boolean to see if we already execute the method get comments
		private var __commentsChecked : Boolean = false;
		private var __tags : Array = [];
		
		/**
		 * Constructor.
		 * 
		 * @param id The id of your post
		 * @param url The url of your post
		 * @param source The XML representing your post
		 * */
		public function WPPost(id : String, url : String, source : * = null)
		{
			super(id, url, source);
			multiplePostPossible = false;
			if (source) {
				__setCategories();
				__setTags();
			}
		}
		
		private function __setCategories() : void {
			__setCategoriesBySelector(WPTheme.POST_CATEGORY_SELECTOR);
		}
		
		private function __setTags() : void {
			__setCategoriesBySelector(WPTheme.POST_TAG_SELECTOR);
		}
		
		private function __setCategoriesBySelector(selector : String) : void {
			var categoryLinks : Array = $$(selector);
			var len : int = categoryLinks.length;
			for (var i : int = 0; i < len; i++) {
				var a : Element = categoryLinks[i];
				var category : WPCategory = DocumentManager.getInstance().getAndCreateIfNecessaryCategoryByName(a.innerHTML) as WPCategory;
				if (categories.indexOf(category) == -1) {
					categories.push(category);
				}
			}
		}
		
		/**
		 * Return the wordpress id of your post.
		 * 
		 * @return The wordpress id
		 * */
		public function get postId() : int {
			if (__postId == -1) {
				var element : Element;
				if (isReady) {
					element = $$(WPTheme.POST_ID_SELECTOR)[0] as Element
				} else {
					element = this;
				}
				__postId = int(element.att('id').replace(WPTheme.POST_ID_BASE, ''));
			}
			return __postId;
		}
		
		/**
		 * The title of your post.
		 * 
		 * @return The title
		 * */
		public function get title() : String {
			if (!__title) {
				var element : Element = $$(WPTheme.POST_TITLE_SELECTOR)[0];
				__title = StringUtil.stripTags(element.innerHTML);
			}
			return __title;
		}
		
		/**
		 * The images of your post (img tag).
		 * 
		 * @return The list of ImgElement that are images
		 * */
		override public function get images() : Array /* of ImgElement */ {
			if (!_images) {
				_images = $$(WPTheme.POST_IMAGES_SELECTOR);
			}
			return _images;
		}

		/**
		 * The links of your post (a tag).
		 * 
		 * @return The list of LinkElement that are links
		 * */
		override public function get links() : Array /* of LinkElement */ {
			if (!_links) {
				_links = $$(WPTheme.POST_LINKS_SELECTOR);
			}
			return _links;
		}
		
		/**
		 * The content of your post.
		 * 
		 * @return The Element that represent the content of your post
		 * */
		public function get content() : Element {
			if (!__content) {
				__content = $$(WPTheme.POST_CONTENT_SELECTOR)[0];
			}
			return __content;
		}
				
		/**
		 * The date of your post has shown in your post.
		 * 
		 * @return The date
		 * */
		public function get date() : Date {
			if (!__date) {
				var element : Element = $$(WPTheme.POST_DATE_SELECTOR)[0];
				__date = WPDateUtil.dateFromString(element.innerHTML, false);
			}
			return __date;
		}
		
		/**
		 * The list of comment that are linked to your post.
		 * 
		 * <p>Before you call this method, you will need to load the document first, by using
		 * for example api.loadSelectedDocument().
		 * </p>
		 * 
		 * @return The list of WPComment
		 * */
		public function get comments() : Array /* of WPComment */{
			if (!__commentsChecked) {
				var res : Array = [];
				var list : Array = $$(WPTheme.POST_COMMENTS_SELECTOR);
				var len : int = list.length;
				for (var i : int = 0; i < len; i++) {
					var c : WPComment = new WPComment(this, (list[i] as Element).source);
					res.push(c);
				}
				__commentsChecked = true;
				__comments = __comments.concat(res);
			}
			return __comments;
		}
		
		/**
		 * Return the number of comments on your post.
		 * <p>This value is not linked to the length of comments but to the value in the XHTML</p>
		 * 
		 * @return The number of comments
		 * */
		public function get numComments() : int {
			if (__numComments == -1) {
				var list : Array = $$(WPTheme.POST_NUM_COMMENTS_SELECTOR);
				var a : Element = list[list.length - 1];
				var content : String = a.innerHTML;
				if (content.substr(0, 2) == WPTheme.POST_NUM_COMMENTS_NO_LANG) {
					__numComments = 0;
				} else if (content.substr(0, 1) == WPTheme.POST_NUM_COMMENTS_ONE_LANG) {
					__numComments = 1;
				} else {
					var str : String = content.replace(WPTheme.POST_NUM_COMMENTS_COMMENTS_LANG, '');
					__numComments = int(str);
				}
			}
			return __numComments;
		}
		
		/**
		 * @private
		 * */ 
		public function sendComment(content : String, commenterPseudo : String, commenterEmail : String, commenterWebsite : String = "") : WPComment {
			var comment : WPComment = new WPComment(this);
			comment.user = new WPUser(this);
			comment.user.email = commenterEmail;
			comment.user.pseudo = commenterPseudo;
			comment.user.website = new LinkElement(this, <a href={commenterWebsite} />);
			comment.user.avatar = new ImgElement(this, <img src={WPTheme.USER_AVATAR_DEFAULT} />);
			comment.content = content;
			var res : Boolean = comment.send();
			if (res) {
				comment.addEventListener(WPCommentEvent.COMMENT_SUCCEED, __commentHandler);
				comment.addEventListener(WPCommentEvent.COMMENT_FAILED, __commentHandler);
			} 
			return res ? comment : null;
		} 
		
		private function __commentHandler(e : WPCommentEvent) : void {
			if (e.type == WPCommentEvent.COMMENT_SUCCEED) {
				__comments.push(e.currentTarget as WPComment);
				__numComments++;
			}
			dispatchEvent(e.clone());
		}
		
	}
}